﻿using furion.blog.Application.Contracts;
using furion.blog.Domain.Shared.Attribute;
using furion.blog.Utils;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Masuit.Tools;
using Microsoft.AspNetCore.Mvc;

namespace furion.blog.Web.Entry.Api
{
    /// <summary>
    /// 系统日志相关接口
    /// </summary>
    [ApiDescriptionSettings(Groups = new string[] { "RequestLogApi" })]
    [Route("api/requestLog")]
    public class RequestLogApiController : IDynamicApiController
    {
        private readonly IRequestLogService _requestLogService;
        public RequestLogApiController(IRequestLogService requestLogService)
        {
            this._requestLogService = requestLogService;
        }

        [ActionAttribute(Name = "请求日志详情接口", Desc = "请求日志详细信息")]
        [HttpGet, Route("{date}/{id}")]
        public async Task<IActionResult> detail([FromRoute(Name = "date")]string date,[FromRoute(Name = "id")]long id) {
            var ret = await this._requestLogService.Detail(date,id);
            return new OkObjectResult(ResultUtil.Success(ret));
        }

        [ActionAttribute(Name = "请求日志分页接口", Desc = "请求日志分页数据")]
        [HttpGet, Route("list")]
        public async Task<IActionResult> list(
          [FromQuery(Name = "page")] int pageIndex,
          [FromQuery(Name = "limit")] int pageSize,
          [FromQuery(Name = "searchContent")] string searchContent = "",
          [FromQuery(Name = "date")] string date = "",
          [FromQuery(Name = "role")] string role = "",
          [FromQuery(Name = "requestMethod")] string requestMethod = ""
          )
        {
            if (string.IsNullOrWhiteSpace(date))
            {
                throw Oops.Oh("缺少日期参数");
            }
            var ret = await this._requestLogService.List(new Domain.Query.RequestLog.RequestLogListQuery
            { 
                PageIndex = pageIndex, 
                PageSize = pageSize , 
                SearchContent = searchContent , 
                Date = date ,
                Role = role,
                RequestMethod = requestMethod 
            });
            return new OkObjectResult(ResultUtil.Success(ret));
        }

    }
}
